// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liuxinlong.enums.ExpertGroupEnum;
import com.liuxinlong.modules.dao.ExpertDao;
import com.liuxinlong.modules.entity.Expert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liuxinlong.common.TreeNode;
import com.liuxinlong.modules.common.service.TreeService;
import com.liuxinlong.modules.dao.HierarchyDao;
import com.liuxinlong.modules.dao.MenuDao;
import com.liuxinlong.modules.entity.Hierarchy;
import com.liuxinlong.modules.entity.Menu;

import lombok.extern.slf4j.Slf4j;

/**
 * 树查询服务层实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月26日
 */
@Service
@Slf4j
public class TreeServiceImpl implements TreeService {

	@Autowired
	private HierarchyDao hierarchyDao;

	@Autowired
	private MenuDao menuDao;

	@Autowired
	private ExpertDao expertDao;

	@Override
	public TreeNode queryHierarchyTree(int actionScope) {
		boolean checkFlag = actionScope == 1;
		List<Hierarchy> hierarchyList = hierarchyDao.getHierarchyList();
		List<TreeNode> bodyList = new ArrayList<>();
		for (Hierarchy hierarchy : hierarchyList) {
			if(checkFlag && hierarchy.getActionScope()!=1) {
				continue;
			}
			TreeNode treeNode = new TreeNode();
			treeNode.setId(hierarchy.getId());
			treeNode.setName(hierarchy.getName());
			treeNode.setValue(hierarchy.getId());
			treeNode.setLabel(hierarchy.getName());
			treeNode.setTitle(hierarchy.getName());
			treeNode.setParentId(hierarchy.getParentId());
			treeNode.setOther(String.valueOf(hierarchy.getActionScope()));
			bodyList.add(treeNode);
		}
		TreeNode root = new TreeNode();
		root.setId("0");
		root.setName("AOS");
		root.setValue("0");
		root.setLabel("AOS");
		root.setTitle("AOS");
		root.setChildren(new ArrayList<>());
		getChild(root, bodyList, new HashMap<>(), 1, 0);
		return root;
	}

	@Override
	public void getChild(TreeNode parent, List<TreeNode> bodyList, Map<String, String> map, int currentDeep,
			int targetDeep) {
		List<TreeNode> childList = new ArrayList<>();
		int nextDeep = currentDeep + 1;
		if (targetDeep != 0 && currentDeep == targetDeep) {
			parent.setChildren(childList);
			return;
		}
		bodyList.stream().filter(c -> !map.containsKey(c.getId())).filter(c -> StringUtils.equals(c.getParentId(),parent.getId()))
				.forEach(c -> {
					map.put(c.getId(), c.getParentId());
					this.getChild(c, bodyList, map, nextDeep, targetDeep);
					childList.add(c);
				});
		parent.setIsLeaf(childList.isEmpty());
		parent.setChildren(childList);
	}

	@Override
	public TreeNode queryMenuTree() {
		List<Menu> menusList = menuDao.queryMenuList();
		List<TreeNode> bodyList = new ArrayList<>();
		for (Menu menu : menusList) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(menu.getId());
			treeNode.setKey(menu.getId());
			treeNode.setName(menu.getName());
			treeNode.setValue(menu.getId());
			treeNode.setLabel(menu.getName());
			treeNode.setTitle(menu.getName());
			treeNode.setParentId(menu.getParentId());
			treeNode.setOther(String.valueOf(menu.getRightType()));
			bodyList.add(treeNode);
		}
		TreeNode root = new TreeNode();
		root.setId("0");
		root.setKey("0");
		root.setName("PMIP");
		root.setValue("0");
		root.setLabel("PMIP");
		root.setTitle("PMIP");
		root.setOther("0");
		root.setChildren(new ArrayList<>());
		getChild(root, bodyList, new HashMap<>(), 1, 0);
		return root;
	}

	@Override
	public TreeNode queryExpertGroupTree() {
		List<Expert> expertList = expertDao.queryExpertList(0);
		List<TreeNode> bodyList = new ArrayList<>();
		for (Expert expert : expertList) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(expert.getId());
			treeNode.setKey(expert.getId());
			treeNode.setName(expert.getName());
			treeNode.setValue(expert.getId());
			treeNode.setLabel(expert.getName());
			treeNode.setTitle(expert.getName());
			treeNode.setParentId(expert.getArea());
			treeNode.setOther("2");
			bodyList.add(treeNode);
		}
		for(ExpertGroupEnum groupEnum : ExpertGroupEnum.values()) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(String.valueOf(groupEnum.getValue()));
			treeNode.setKey(String.valueOf(groupEnum.getValue()));
			treeNode.setName(groupEnum.getName());
			treeNode.setValue(String.valueOf(groupEnum.getValue()));
			treeNode.setLabel(groupEnum.getName());
			treeNode.setTitle(groupEnum.getName());
			treeNode.setParentId("0");
			treeNode.setOther("1");
			bodyList.add(treeNode);
		}
		TreeNode root = new TreeNode();
		root.setId("0");
		root.setKey("0");
		root.setName("专家组");
		root.setValue("0");
		root.setLabel("专家组");
		root.setTitle("专家组");
		root.setOther("0");
		root.setChildren(new ArrayList<>());
		getChild(root, bodyList, new HashMap<>(), 1, 0);
		root.setParamOne(String.valueOf(expertList.size()));
		return root;
	}

}
